package com.yumi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yumi.common.Result;
import com.yumi.entity.PerformanceAppraisal;
import com.yumi.service.PerformanceAppraisalService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 绩效考核控制器
 */
@RestController
@RequestMapping("/api/performances")
@Slf4j
@Tag(name = "绩效考核接口", description = "绩效考核管理相关接口")
public class PerformanceAppraisalController {

    @Autowired
    private PerformanceAppraisalService performanceAppraisalService;

    /**
     * 新增考核记录
     * @param performanceAppraisal 考核记录信息
     * @return 添加结果
     */
    @PostMapping
    @Operation(summary = "新增考核记录", description = "新增员工绩效考核记录")
    public Result<Boolean> addPerformanceAppraisal(@RequestBody @Valid PerformanceAppraisal performanceAppraisal) {
        log.info("新增考核记录：{}", performanceAppraisal);
        boolean result = performanceAppraisalService.addPerformanceAppraisal(performanceAppraisal);
        return Result.success(result);
    }

    /**
     * 分页查询考核记录
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @param employeeId 员工ID
     * @param grade 考核等级
     * @return 考核记录分页结果
     */
    @GetMapping
    @Operation(summary = "分页查询考核记录", description = "根据条件分页查询绩效考核记录")
    public Result<Page<PerformanceAppraisal>> getPerformanceAppraisalPage(
            @RequestParam(value = "page", defaultValue = "1") @Parameter(description = "页码") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") @Parameter(description = "每页大小") Integer pageSize,
            @RequestParam(value = "employeeId", required = false) @Parameter(description = "员工ID") Long employeeId,
            @RequestParam(value = "grade", required = false) @Parameter(description = "考核等级") String grade) {
        log.info("分页查询考核记录，页码：{}，每页大小：{}，员工ID：{}，考核等级：{}", pageNum, pageSize, employeeId, grade);
        Page<PerformanceAppraisal> page = new Page<>(pageNum, pageSize);
        Page<PerformanceAppraisal> result = performanceAppraisalService.getPerformanceAppraisalPage(page, employeeId, grade);
        return Result.success(result);
    }

    /**
     * 查询考核详情
     * @param id 考核ID
     * @return 考核详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询考核详情", description = "根据ID查询绩效考核详情")
    public Result<PerformanceAppraisal> getPerformanceAppraisalById(
            @PathVariable("id") @Parameter(description = "考核ID") Long id) {
        log.info("查询考核详情，ID：{}", id);
        PerformanceAppraisal performanceAppraisal = performanceAppraisalService.getPerformanceAppraisalById(id);
        return Result.success(performanceAppraisal);
    }

    /**
     * 修改考核记录
     * @param id 考核ID
     * @param performanceAppraisal 考核记录信息
     * @return 修改结果
     */
    @PutMapping("/{id}")
    @Operation(summary = "修改考核记录", description = "根据ID修改绩效考核记录")
    public Result<Boolean> updatePerformanceAppraisal(
            @PathVariable("id") @Parameter(description = "考核ID") Long id,
            @RequestBody @Valid PerformanceAppraisal performanceAppraisal) {
        log.info("修改考核记录，ID：{}，数据：{}", id, performanceAppraisal);
        performanceAppraisal.setId(id);
        boolean result = performanceAppraisalService.updatePerformanceAppraisal(performanceAppraisal);
        return Result.success(result);
    }

    /**
     * 删除考核记录
     * @param id 考核ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除考核记录", description = "根据ID删除绩效考核记录")
    public Result<Boolean> deletePerformanceAppraisal(
            @PathVariable("id") @Parameter(description = "考核ID") Long id) {
        log.info("删除考核记录，ID：{}", id);
        boolean result = performanceAppraisalService.deletePerformanceAppraisal(id);
        return Result.success(result);
    }

    /**
     * 获取员工考核历史
     * @param employeeId 员工ID
     * @return 员工考核历史列表
     */
    @GetMapping("/employee/{employeeId}")
    @Operation(summary = "员工考核历史", description = "根据员工ID查询考核历史记录")
    public Result<List<PerformanceAppraisal>> getEmployeePerformanceHistory(
            @PathVariable("employeeId") @Parameter(description = "员工ID") Long employeeId) {
        log.info("查询员工考核历史，员工ID：{}", employeeId);
        List<PerformanceAppraisal> list = performanceAppraisalService.getEmployeePerformanceHistory(employeeId);
        return Result.success(list);
    }

    /**
     * 导出绩效考核数据到Excel
     * @param employeeId 员工ID（可选）
     * @param grade 考核等级（可选）
     * @param response HTTP响应对象
     */
    @Operation(summary = "导出绩效考核数据", description = "导出绩效考核数据到Excel文件")
    @GetMapping("/export")
    public void exportToExcel(
            @RequestParam(value = "employeeId", required = false) @Parameter(description = "员工ID") Long employeeId,
            @RequestParam(value = "grade", required = false) @Parameter(description = "考核等级") String grade,
            HttpServletResponse response) {
        log.info("导出绩效考核数据，员工ID：{}，考核等级：{}", employeeId, grade);
        
        try {
            performanceAppraisalService.exportToExcel(employeeId, grade, response);
        } catch (IOException e) {
            log.error("导出绩效考核数据失败", e);
        }
    }
} 